package org.vin.tempctl;


import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;

public class Temperature {

    public static int getTemperature(ModbusMaster master) throws Exception{

        int slaveId = 1;//从机地址
        int offset = 2;//寄存器读取开始地址
        int quantity = 1;//读取的寄存器数量
        int[] registerValues;

        try {
            registerValues = master.readHoldingRegisters(slaveId, offset, quantity);
            // 控制台输出
            for (int value : registerValues) {
                System.out.println("Address: " + offset++ + ", Value: " + value);
            }
        } catch (ModbusProtocolException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusNumberException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusIOException e) {
            e.printStackTrace();
            throw e;
        }
        return registerValues[0];//2  温度测量值  RO  0.1
    }

    public static boolean setTemperature(int unit,ModbusMaster master) throws Exception{

        int slaveId = 1;//从机地址
        int offset = 11;//11  温度设定值  RW 0.01 在斜率作用下运行时会发生变化当前设定值
        //int quantity = 1;//读取的寄存器数量
        boolean ret = false;

        try {
            master.writeSingleRegister(slaveId, offset, unit);


        } catch (ModbusProtocolException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusNumberException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusIOException e) {
            e.printStackTrace();
            throw e;
        }
        ret = true;
        System.out.println("Ret: "+ret);
        return ret;
    }

    public static boolean setTemrate(int unit,ModbusMaster master) throws Exception{

        int slaveId = 1;//从机地址
        int offset = 60;//60  温度斜率  RW  0.1
        //int quantity = 1;//读取的寄存器数量
        boolean ret = false;

        try {
            master.writeSingleRegister(slaveId, offset, unit);


        } catch (ModbusProtocolException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusNumberException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusIOException e) {
            e.printStackTrace();
            throw e;
        }
        ret = true;
        System.out.println("Ret: "+ret);
        return ret;
    }


    public static boolean seton(ModbusMaster master) throws Exception{

        int slaveId = 1;//从机地址
        int offset = 1;//寄存器开始地址
        int unit = 3;//寄存器

        try {
            master.writeSingleRegister(slaveId, offset, unit);


        } catch (ModbusProtocolException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusNumberException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusIOException e) {
            e.printStackTrace();
            throw e;
        }
        System.out.println("Ret:true ");
        return true;
    }

    public static boolean setoff(ModbusMaster master) throws Exception{

        int slaveId = 1;//从机地址
        int offset = 1;//寄存器开始地址
        int unit = 2;//寄存器

        try {
            master.writeSingleRegister(slaveId, offset, unit);


        } catch (ModbusProtocolException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusNumberException e) {
            e.printStackTrace();
            throw e;
        } catch (ModbusIOException e) {
            e.printStackTrace();
            throw e;
        }
        System.out.println("Ret:true ");
        return true;
    }


}
